//
//  SPParkingController.m
//  Sport
//
//  Created by tikpen on 2018/5/16.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPParkingController.h"
#import "FVSeatItem.h"
#import "FVSeatsPicker.h"
#import "SPSeatsTitleView.h"

@interface SPParkingController ()<FVSeatsPickerDelegate>
@property (nonatomic, strong) FVSeatsPicker *seatsPicker;
@property (nonatomic, strong) NSMutableArray<FVSeatItem *> *seatsInfo;
@property (nonatomic, copy) NSString *areaId;
@property (nonatomic, copy) NSString *districtName;
@property (nonatomic, copy) NSString *seatMaxX;
@property (nonatomic, copy) NSString *seatMaxY;
@property (nonatomic, copy) NSString *maxSeatNum;
@property (nonatomic, strong) SPSeatsTitleView *seatsTitleView;
@property (nonatomic,assign) int carAddX;
@property (nonatomic,assign) int carAddY;
@property (nonatomic,assign) int carParkLayer;
@property (nonatomic,assign) int car_id;
@end

@implementation SPParkingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configuration];
    [self setBasic];
    [self loadData];
}

- (void)configuration{
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(rightItemAction)];
    [self.navigationItem setRightBarButtonItem:rightBarItem];
    SPSeatsTitleView * seatsTitleView = [[[NSBundle mainBundle]loadNibNamed:@"SPSeatsTitleView" owner:nil options:nil] lastObject];
    [self.view addSubview:seatsTitleView];
    seatsTitleView.frame = CGRectMake(0, 30, SCREEN_WIDTH, 60);
    self.seatsTitleView = seatsTitleView;
}
- (void)rightItemAction{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *user_id = [userDefaults objectForKey:@"ID"];
    
    NSString *url = [NSString stringWithFormat:@"http://120.78.186.122/JT/StopCar.php"];
    NSDictionary *dict = @{
                           @"user_id":user_id,
                           @"carParkNum":self.areaId,
                           @"car_id":@(self.car_id),
                           @"carParkLayer":@1,
                           @"carAddX":@(self.carAddX),
                           @"carAddY":@(self.carAddY)
                           };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", @"text/plain",nil];
    [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *success = responseObject[@"error"];
        NSLog(@"--%@",success);
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认车位" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self loadData];
        }];
        [alert addAction:doneAction];
        [self presentViewController:alert animated:YES completion:nil];
        //        [self loadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"提交车位错误%@",error);
    }];
}

- (void)setBasic{
    self.navigationItem.title = @"停车区域";
    FVSeatsPicker *seatPicker = [[FVSeatsPicker alloc]init];
    seatPicker.seatsDelegate = self;
    seatPicker.backgroundColor = [UIColor whiteColor];
    seatPicker.cellSize = CGSizeMake(24, 24);
    seatPicker.minimumZoomScale = 1;
    seatPicker.maximumZoomScale = 2;
    [seatPicker setImage:[UIImage imageNamed:@"可选"] forState:UIControlStateNormal];
    [seatPicker setImage:[UIImage imageNamed:@"已选"] forState:UIControlStateDisabled];
    [seatPicker setImage:[UIImage imageNamed:@"选中"] forState:UIControlStateSelected];
    seatPicker.frame = CGRectMake(0, CGRectGetMaxY(self.seatsTitleView.frame), SCREEN_WIDTH, SCREEN_HEIGHT * 0.6);
    [self.view addSubview:seatPicker];
    self.seatsPicker = seatPicker;
}
//http://120.78.186.122/JT/testMap.php
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"http://120.78.186.122/JT/DetailParkInfo.php"];
    NSDictionary *dict = @{
                           @"carParkName":@"中海国际停车场"
                           };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json", @"text/javascript", @"text/plain",nil];
    [manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *str = responseObject;
        NSLog(@"%@",str);
        self.areaId = [NSString stringWithFormat:@"%@",responseObject[@"areaid"]];
        NSLog(@"areaId--%@",self.areaId);
        self.districtName = [NSString stringWithFormat:@"%@",responseObject[@"name"]];
        self.seatMaxX = [NSString stringWithFormat:@"%@",responseObject[@"X"]];
        self.seatMaxY = [NSString stringWithFormat:@"%@",responseObject[@"Y"]];
        self.maxSeatNum = [NSString stringWithFormat:@"%@",responseObject[@"max_num"]];
        NSArray *seatsArr = responseObject[@"seats"];
        NSMutableArray *array = [NSMutableArray array];
        for (NSDictionary *dic in seatsArr) {
            FVSeatItem *seatItem = [FVSeatItem mj_objectWithKeyValues:dic];
            [array addObject:seatItem];
        }
        self.seatsInfo = array;
        self.seatsPicker.rowCount = [self.seatMaxY intValue];
        self.seatsPicker.colCount = [self.seatMaxX intValue];
        self.seatsPicker.seats = self.seatsInfo;
        [self.seatsPicker reloadData];
        NSLog(@"成功--%@",seatsArr);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

- (void)seatsPicker:(FVSeatsPicker *)picker didSelectSeat:(FVSeatItem *)seatInfo{
    NSLog(@"%@",seatInfo);
    self.car_id = seatInfo.seatId;
    self.carAddX = seatInfo.coordinateX;
    self.carAddY = seatInfo.coordinateY;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
